package com.yystudy.system.handler;

import com.yystudy.common.result.Result;
import com.yystudy.common.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
@ResponseBody
public class GlobalExceptionalHandler {
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail();
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("执行了特定异常处理");
    }

    @ExceptionHandler(CustomException.class)
    public Result error(CustomException e) {
        e.printStackTrace();
        return Result.fail().message(e.getMessage()).code(e.getCode());

    }


    /**
     * spring security权限异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.fail()
                .code(ResultCodeEnum.PERMISSION.getCode())
                .message("没有当前功能的操作权限");
    }


}
